package com.papper.common.service.sys;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.papper.common.bean.sys.SysPageConfig;
import com.papper.common.cache.CacheConstants;
import com.papper.common.cache.redis.CacheClosureNullDefault;
import com.papper.common.mapper.sys.SysPageConfigMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.StringTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

/**
 * @Author:LaJiao
 * @Date:2019-07-17 15:27:38
 * @Description:系统页面配置
 */
@Service("sysPageConfigService")
public class SysPageConfigService extends ServiceImpl<SysPageConfigMapper, SysPageConfig> {

	@Override
	protected void clearCaches(SysPageConfig entity) {
		super.clearCaches(entity);
		cache.delete(CacheConstants.Keys(getModel(), entity.getSystemCode(), entity.getUserId(), entity.getPageCode()));
	}

	/**
	 * Find by payge code sys page config.
	 *
	 * @param sysCode  the sys code
	 * @param userId   the user id
	 * @param pageCode the page code
	 * @return the sys page config
	 */
	public SysPageConfig findByPaygeCode(final String sysCode, final String userId, final String pageCode) {
		if (StringUtil.isEmpty(sysCode) || StringUtil.isEmpty(userId) || StringUtil.isEmpty(pageCode)) {
			return null;
		}
		return cache.getAndSet(new CacheClosureNullDefault<SysPageConfig>() {
			@Override
			public String getKey() {
				return CacheConstants.Keys(getModel(), sysCode, userId, pageCode);
			}

			@Override
			public SysPageConfig getValue() {
				Example          example  = new Example(SysPageConfig.class);
				Example.Criteria criteria = example.createCriteria();
				criteria.andEqualTo("systemCode", sysCode);
				criteria.andEqualTo("userId", userId);
				criteria.andEqualTo("pageCode", pageCode);
				return findOne(example);
			}

			@Override
			public Class<SysPageConfig> getTypeReference() {
				return SysPageConfig.class;
			}
		});
	}

	/**
	 * Find show item string.
	 *
	 * @param sysId    the sys id
	 * @param userid   the userid
	 * @param pageCode the page code
	 * @return the string
	 */
	public String findShowItem(String sysId, String userid, String pageCode) {
		SysPageConfig entity = findByPaygeCode(sysId, userid, pageCode);
		List<String>  rtn    = Lists.newArrayList();
		if (entity != null) {
			JSONObject pageConfig = JSON.parseObject(entity.getPageConfig());
			for (String k : pageConfig.keySet()) {
				if ("1".equals(pageConfig.getString(k))) {
					rtn.add(k);
				}
			}
		}
		return StringTool.ListStringToString(rtn, ",");
	}

	/**
	 * 删除系统对应配置信息.
	 *
	 * @param sysCode the sys code
	 */
	public void deleteBySysCode(String sysCode) {
		Example          example  = new Example(SysPageConfig.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("systemCode", sysCode);
		delete(example);
	}

	/**
	 * 删除系统对应配置信息.
	 *
	 * @param userId the sys code
	 */
	public void deleteByUser(Long userId) {
		Example          example  = new Example(SysPageConfig.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", userId);
		delete(example);
	}
}